package com.example.mynote;

import androidx.annotation.LongDef;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

public class EditActivity extends BaseActivity {
    private EditText et_content;
    private Toolbar toolbar;
    private Spinner spinner;

    private StringBuilder stringBuilder;
    private long id; // 笔记id
    private String old_content = "";
    private String content;

    private String old_time = "";
    private int openMode; // 0是新建笔记，1是编辑笔记
    private boolean tagShift = false;
    private boolean backDown = false;
    private List<String> tagList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_layout);
        // 获取EditText控件
        et_content = findViewById(R.id.et_content);


        //设置Toolbar
        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_back_24);
        }

        // 获取界面数据
        Intent intent = getIntent();
        openMode = intent.getIntExtra("mode",0);
        if(openMode == 1){
            id = intent.getLongExtra("id",0);
            old_content = intent.getStringExtra("content");
            old_time = intent.getStringExtra("time");
            // 初始化界面
            showNoteDetail(old_content);
        }
        //spinner数据
        tagList = new ArrayList<>();
        tagList.add("学习");
        tagList.add("时间");

    }

    // 设置菜单

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.edit_menu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    // 通过按键来是否接束活动
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_HOME ) return true;
        else if(keyCode == KeyEvent.KEYCODE_BACK){
            Intent intent = autoSetMessage();
            setResult(RESULT_OK,intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    // 返回上个活动的数据
    private Intent autoSetMessage() {
        Intent intent = new Intent();
        // 获取最新内容
        content = et_content.getText().toString();
        if(openMode == 1){ // 编辑：只需要考虑修改的笔记
            if(!content.equals(old_content))
            {
                intent.putExtra("mode",3); // 修改3
                intent.putExtra("id",id);
                intent.putExtra("content",content);
                intent.putExtra("time",dataToStr());
            }
            // 内容没修改，无需改动
        }else { // 新建
            intent.putExtra("id",id);
            intent.putExtra("content",content);
            intent.putExtra("time",dataToStr());
        }
        return intent;
    }

    // 时间格式化
    public String dataToStr(){
        /* 年yyyy 月MM 日dd 时HH 分mm 秒ss*/
        Date date = new Date();
        SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleFormatter.format(date);
    }
    // 将笔记内容展示到界面
    public void showNoteDetail(String str){
        et_content.setText(str);
        et_content.setSelection(str.length()); // 光标定位末尾
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //处理Toolbar各个按钮的点击事件
        switch (item.getItemId()) {
            // HomeAsUp按钮的id永远都是android.R.id.home
            case android.R.id.home:
                Intent intent = autoSetMessage();
                setResult(RESULT_OK,intent);
                finish();
                break;
            case R.id.delete:
                new AlertDialog.Builder(EditActivity.this)
                        .setMessage("确认要删除这条笔记？")
                        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //如果是编辑界面点击删除，则删除原本存在的笔记，新建页面直接关闭
                                if(openMode == 1){
                                    // 返回上一个活动 并将当前活动笔记id传过去
                                    Intent delIntent = new Intent();
                                    delIntent.putExtra("id",id);
                                    delIntent.putExtra("mode",2); // 2表示删除
                                    setResult(RESULT_OK,delIntent);
                                }
                                // 结束当前活动
                                finish();
                            }
                        })
                        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        }).create().show();
                break;
            default:
        }
        return true;
    }
}